﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace Ruandao.MiniAccess
{
    /// 在配置文件中设定provider的类型和对象的连接字符串，各个provider组成一个字典。
    /// 自定义dataProvider配置节，不再使用appSettings。
    /// <![CDATA[
    /// <miniAccess>
    ///     <dataProviders>
    ///         <add name="" accessType="" connectionString="" dataSourceSetter="" />
    ///     </dataProviders>
    /// </miniAccess>
    /// ]]>
    /// accessType和connectionString属性为必填项，表示缺省的值。
    /// dataProviderClass属性可选，当指定dataProviderClass时所得到的值覆盖缺省值。
    public class MiniAccessSection : ConfigurationSection
    {
        public MiniAccessSection()
        {
            // Create the element.
        }

        // Declare a collection element represented 
        // in the configuration file by the sub-section
        // <urls> <add .../> </urls> 
        // Note: the "IsDefaultCollection = false" 
        // instructs the .NET Framework to build a nested 
        // section like <urls> ...</urls>.
        [ConfigurationProperty("dataProviders",
            IsDefaultCollection = false)]
        public ProviderCollection DataProviders
        {

            get
            {
                ProviderCollection providersCollection =
                (ProviderCollection)base["dataProviders"];
                return providersCollection;
            }
        }

    }

    public class ProviderElement : ConfigurationElement
    {
        public ProviderElement()
        {
        }

        public ProviderElement(String name)
        {
            Name = name;
        }

        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        public string Name
        {
            get
            {
                return (string)this["name"];
            }
            set
            {
                this["name"] = value;
            }
        }

        [ConfigurationProperty("dataSourceSetter", IsRequired = false)]
        public string DataSourceSetter
        {
            get
            {
                return (string)this["dataSourceSetter"];
            }
            set
            {
                this["dataSourceSetter"] = value;
            }
        }

        [ConfigurationProperty("accessType", IsRequired = true)]
        public string AccessType
        {
            get
            {
                return (string)this["accessType"];
            }
            set
            {
                this["accessType"] = value;
            }
        }

        [ConfigurationProperty("connectionString", IsRequired = true)]
        public string ConnectionString
        {
            get
            {
                return (string)this["connectionString"];
            }
            set
            {
                this["connectionString"] = value;
            }
        }
    }

    public class ProviderCollection : ConfigurationElementCollection
    {
        public ProviderCollection()
        {
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new ProviderElement();
        }


        protected override ConfigurationElement CreateNewElement( string elementName)
        {
            return new ProviderElement(elementName);
        }


        protected override Object GetElementKey(ConfigurationElement element)
        {
            return ((ProviderElement)element).Name;
        }


        public new string AddElementName
        {
            get
            { return base.AddElementName; }

            set
            { base.AddElementName = value; }

        }

        public new string ClearElementName
        {
            get
            { return base.ClearElementName; }

            set
            { base.AddElementName = value; }

        }

        public new string RemoveElementName
        {
            get
            { return base.RemoveElementName; }


        }

        public new int Count
        {

            get { return base.Count; }

        }


        public ProviderElement this[int index]
        {
            get
            {
                return (ProviderElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        new public ProviderElement this[string Name]
        {
            get
            {
                return (ProviderElement)BaseGet(Name);
            }
        }

        public int IndexOf(ProviderElement url)
        {
            return BaseIndexOf(url);
        }

        public void Add(ProviderElement url)
        {
            BaseAdd(url);

            // Add custom code here.
        }

        public void Remove(ProviderElement url)
        {
            if (BaseIndexOf(url) >= 0)
                BaseRemove(url.Name);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void Clear()
        {
            BaseClear();
            // Add custom code here.
        }
    }

}
